/*
 * jQuery plugin for the CFmaker framework
 * the intention is to roll all client-side functionality (where ever it makes sense) in jQuery functions
 * we also utilize CFC proxies, which depend on the <cfajaxproxy> tags in the Application.cfc
 */

/*
 * this self-executor sets up the custom, jQuery functions
 */
(function($, applicationProxy, controllerProxy, displayProxy) {
	var builtinAttr = "val,height,offset,offsetParent,outerHeight,outerWidth,position,text,width";
	
	$.fn.com = function(comFn, thisAttr) {
		var attrStr = "" || thisAttr;
		var controllerName = controllerDisplay + "?method=" + comFn;
		var localVal = "";
		
		// if the supplied attribute has a corresponding jQuery function, then use it (since jQuery has more functionality than just HTML attributes)
		if (attrStr.length) {
			if (builtinAttr.indexOf(thisAttr, 0) < 0) {
				// this is not a built-in attribute function
				attrStr = "attr('" + thisAttr + "')";
			} else {
				attrStr = thisAttr + "()";
			}
		}
		
		return this.each(function() {
			var localVal = eval("$(this)." + attrStr);
			$.getJSON(controllerName, thisAttr, function(data) {
				$.data(controllerName, data);
			});
		});
	}
	
	$.fn.display = function(viewName, args) {
		var argStr = "";
		var content = "";
		var I = "";
		
		for (I in args) {
			argStr += "'" + args[I] + "',";
		}
		
		argStr = argStr.substr(0, argStr.length-1);	// strip off the trailing comma
		content = eval("app.disp." + viewName + "(" + argStr + ")");
		
		$.getJSON(displayProxy + "?method=" + viewName, args, function(data) {
			return this.each(function() {
				$(this).html(loadingDiv);
				$(this).html(data);
			});
		});
		
		return this.html(content);
		
		return this.each(function() {
			// for now...later we check the element's type and replace it with a similar element
			$(this).html(loadingDiv);
			$(this).html(content);
		});
	}
	
/*	
	$.fn.myPlugin = function(settings) {
	     var config = {'foo': 'bar'};
	 
	     if (settings) $.extend(config, settings);
	 
	     this.each(function() {
	       // element-specific code here
	     });
	 
	     return this;
	 
	   };
*/

	// helper fn for console logging
	// set $.fn.ajaxSubmit.debug to true to enable debug logging
	// borrowed from jQuery.form ... trying to stay with convention
	function log() {
		if ($.fn.ajaxSubmit.debug && window.console && window.console.log)
			window.console.log('[jquery.CFmaker] ' + Array.prototype.join.call(arguments,''));
	};
})(jQuery, applicationProxy, controllerProxy, displayProxy);